#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>

int main()
{
    int status,pid;
    int ppid=getpid(); //获取父进程
    printf("rootpid=%d\n",ppid);
    int son=-1;

    for(int i=0;i<3;i++){///生成三个进程
        
        if( ppid == getpid() )
        {
           int res=fork(); 
           
           if(i==0&&res==0){//记录第一个子进程的pid
              son=getpid();
           }           
        }
    } 
    lockf(1,1,0);
    printf("lock room:%d\n",getpid());
    lockf(1,0,0);
    //给第一个子进程创建子进程
    if(getpid()==son){
       fork();
    }

    while( (pid= waitpid(-1,&status,0))>0 )
    {
         if(WIFEXITED(status))
         {
            printf("child %d terminated nomally with exit status = %d\n",pid,WIFEXITED(status));
         }
         else printf("child %d terminated abnormally\n",pid);
    }

    printf("mypid is : %d ; myfather pid is : %d\n",getpid(),getppid());

    exit(0);

}
